<template>
  <div class="page release-setting">
    <div>
      <group>
        <cell title="无跟进释放设置" is-link :link="{name: 'no-follow-config', query: {team_id: teamId} }" :value="getStatus(releaseConfig, 'is_use_no_follow')"></cell>
        <cell title="无成交释放设置" is-link :link="{name: 'no-deal-config', query: {team_id: teamId} }" :value="getStatus(releaseConfig, 'is_use_no_deal')"></cell>
        <cell title="客户再分配设置" is-link :link="{name: 'rs-rule-config', query: {team_id: teamId} }" :value="getValue(distributeLimit, 'forbid_grab_day') + '天'"></cell>
      </group>
    </div>
  </div>
</template>
<script>
  import Group from 'vux/src/components/group'
  import Cell from 'vux/src/components/cell'
  import { mapActions, mapState } from 'vuex'
  import Q from 'q'

  export default {
    methods: {
      initReleaseConfig () {
        return Q.Promise((resolve, reject) => {
          this.getReleaseConfig(this.teamId).then((response) => {
            if (response.data.errcode === 0) {
              this.releaseConfig = Object.assign(this.releaseConfig, response.data.results)
            }
            this.init = true
            resolve()
          }).catch(() => {
            this.init = true
            resolve()
          })
        })
      },
      initDistributeConfig () {
        return Q.Promise((resolve, reject) => {
          this.getDistributeLimit(this.teamId).then((response) => {
            if (response.data.errcode === 0) {
              this.distributeLimit = response.data.results
              if (this.distributeLimit.data) {
                this.distributeLimit.data.forEach((elem, index) => {
                  this.existsSuids.push(elem.suid)
                })
              }
            }
            this.init = true
            resolve()
          }).catch(() => {
            this.init = true
            resolve()
          })
        })
      },
      getStatus (variable, key) {
        let status = ''
        if (!variable || !Object.keys(variable).length) {
          status = ''
        } else {
          status = variable[key] ? '启用' : '禁用'
        }
        return status
      },
      getValue (variable, key) {
        let value = ''
        if (!variable || !Object.keys(variable).length) {
          value = ''
        } else {
          value = variable[key]
        }
        return value
      },
      initialize () {
        this.$vux.loading.show({text: 'loading'})
        this.initReleaseConfig()
        .then(this.initDistributeConfig).then(() => {
          this.loading.hide()
        }).catch(() => {
          this.$vux.loading.hide()
        })

        this.callJsApi('biz.navigation.setTitle', {title: '客户释放设置'})
      },
      ...mapActions({
        'getReleaseConfig': 'getReleaseConfig',
        'getDistributeLimit': 'getDistributeLimit'
      })
    },
    computed: {
      ...mapState({
        team: state => state.app.user.teams ? state.app.user.teams[0] : []
      })
    },
    watch: {
      team () {
        // if (!this.init) {
        //   this.initialize()
        // }
      }
    },
    components: {
      Group,
      Cell
    },
    mounted () {
      this.teamId = this.$route.query.team_id
    // if (this.team && this.teamId) {
    this.initialize()
    // }
    },
    data () {
      return {
        init: false,
        teamId: '',
        distributeLimit: {},
        releaseConfig: {}
      }
    }
  }
</script>
